<!DOCTYPE html>
<html>

	<head>

		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, initial-scale=1.0">


		<title>端口扫描</title>
		<meta name="keywords" content="Vulnerability scanner">
		<meta name="description" content="maverick0407">

		<link rel="shortcut icon" href="../../static/favicon.ico">
		<link href="../../static/css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
		<link href="../../static/css/font-awesome.css?v=4.4.0" rel="stylesheet">

		<!-- Data Tables -->
		<link href="../../static/css/plugins/dataTables/dataTables.bootstrap.css" rel="stylesheet">
		<link href="../../static/css/animate.css" rel="stylesheet">
		<link href="../../static/css/style.css?v=4.1.0" rel="stylesheet">
		<link href="../../static/css/plugins/bootstrap-table/bootstrap-table.min.css" rel="stylesheet">

	</head>

	<body class="gray-bg">
		<div class="wrapper wrapper-content animated fadeInRight">
			<div class="row">
				<div class="col-sm-12">
					<div class="ibox float-e-margins">
						<div class="ibox-title">
							<h5>请输入url
							</h5>
							<div class="ibox-tools">
								<a class="collapse-link">
									<i class="fa fa-chevron-up"></i>
								</a>

							</div>
						</div>
						<!--			url输入框开始-->
						<div class="ibox-content">
							<form id="scan-form" method="POST" action="{% url 'portscan' %}">
								{% csrf_token %}
								<div class="form-group">
									<label for="url_input">URL:</label>
									<input type="text" class="form-control" id="url_input" name="url_input" required>
								</div>
								<button type="submit" class="btn btn-primary" id="scan-button">开始扫描</button>
							</form>
							<!-- 进度条-->
							<div class="progress-div">
								<div class="progress">
									<div class="progress-bar progress-bar-striped active" id="progress-bar" role="progressbar" aria-valuenow="2" aria-valuemin="0" aria-valuemax="100" style="min-width: 0em; width: 02%;">
										0%
									</div>
								</div>
							</div>



							<div class="progress-text progress-bar-striped active" role="progressbar" aria-valuenow="2" aria-valuemin="0" aria-valuemax="100" style="min-width: 2em; width: 2%;">

							</div>
							<!-- 进度条-->
						</div>
						<!--			url输入框结束-->

					</div>

					<!-- 提示信息元素 -->
					<div id="messages">

						{% for message in messages %}
						<div class="alert alert-{{ message.tags }} alert-dismissible" role="alert">
							<button type="button" class="close" data-dismiss="alert" aria-label="Close">
								<span aria-hidden="true">&times;</span>
							</button>
							{{ message }}
						</div>
						{% endfor %}
					</div>
					<!-- 提示信息元素 -->


					<!-- Panel Other -->
					<div class="ibox float-e-margins">
						<div class="ibox-title">
							<h5>扫描结果</h5>
							<div class="ibox-tools">
								<a class="collapse-link">
									<i class="fa fa-chevron-up"></i>
								</a>

							</div>
						</div>
						<div class="ibox-content">
							<div class="row row-lg">
								<div class="col-sm-12">
									<!-- Example Toolbar -->
									<div class="example-wrap">

										<div class="example">
											<table id="exampleTableToolbar" data-mobile-responsive="true">
												<thead>
													<tr>
														<th data-field="url" style="text-align: center;">目标域名</th>
														<th data-field="port" style="text-align: center;">端口</th>
														<th data-field="status" style="text-align: center;">状态</th>
														<th data-field="scan_time" style="text-align: center;">时间</th>
													</tr>
												</thead>
											</table>
										</div>
									</div>
									<!-- End Example Toolbar -->
								</div>


							</div>
						</div>
					</div>
					<!-- End Panel Other -->


				</div>
			</div>
			<div class="row">
				<div class="col-sm-12">
					<div class="ibox float-e-margins">
						<div class="ibox-title">
							<h5>可编辑表格</h5>
							<div class="ibox-tools">
								<a class="collapse-link">
									<i class="fa fa-chevron-up"></i>
								</a>
							</div>
						</div>
						<div class="ibox-content">
							<div class="">
								<a onclick="fnClickAddRow();" href="javascript:void(0);" class="btn btn-primary ">添加行</a>
							</div>
							<table class="table table-striped table-bordered table-hover " id="editable">
								<thead>
									<tr>
											<tr>
												<th style="text-align: center;">端口号</th>
												<th style="text-align: center;">端口服务</th>
												<th style="text-align: center;">使用的协议</th>
											</tr>
									</tr>
								</thead>
								<tbody>
											{% for port in ports %}
											<tr class="gradeX">
												<th style="text-align: center;">{{port.port_number}}</th>
												<th style="text-align: center;">{{port.service}}</th>
												<th style="text-align: center;">{{port.protocol}}</th>
											</tr>
											{% endfor %}
								</tbody>
							</table>

						</div>
					</div>
				</div>
			</div>
		</div>

		<!-- 全局js -->
		<script src="../../static/js/jquery.min.js?v=2.1.4"></script>
		<script src="../../static/js/bootstrap.min.js?v=3.3.6"></script>



		<script src="../../static/js/plugins/jeditable/jquery.jeditable.js"></script>

		<!-- Data Tables -->
		<script src="../../static/js/plugins/dataTables/jquery.dataTables.js"></script>
		<script src="../../static/js/plugins/dataTables/dataTables.bootstrap.js"></script>
		<script src="../../static/js/plugins/bootstrap-table/bootstrap-table.min.js"></script>
		<script src="../../static/js/plugins/bootstrap-table/bootstrap-table-mobile.min.js"></script>
		<script src="../../static/js/plugins/bootstrap-table/locale/bootstrap-table-zh-CN.min.js"></script>

		<!-- 自定义js -->
		<script src="../../static/js/content.js?v=1.0.0"></script>
		<!-- Peity -->
		<script src="../../static/js/demo/bootstrap-table-demo.js"></script>


		<!-- Page-Level Scripts -->
		<script>
			$(document).ready(function () {
			            $('.dataTables-example').dataTable();

			            /* Init DataTables */
			            var oTable = $('#editable').dataTable();

			            /* Apply the jEditable handlers to the table */
			            oTable.$('td').editable('../example_ajax.php', {
			                "callback": function (sValue, y) {
			                    var aPos = oTable.fnGetPosition(this);
			                    oTable.fnUpdate(sValue, aPos[0], aPos[1]);
			                },
			                "submitdata": function (value, settings) {
			                    return {
			                        "row_id": this.parentNode.getAttribute('id'),
			                        "column": oTable.fnGetPosition(this)[2]
			                    };
			                },

			                "width": "90%",
			                "height": "100%"
			            });


			        });

			        function fnClickAddRow() {
			            $('#editable').dataTable().fnAddData([
			                "Custom row",
			                "New row",
			                "New row",
			                "New row",
			                "New row"]);

			        }
		</script>

<!--		<script type="text/javascript" src="http://tajs.qq.com/stats?sId=9051096" charset="UTF-8"></script>-->
		<!--统计代码，可删除-->


    		<!--此脚本自己写用来用ajax动态插入扫描完成后的数据和显示进度条-->
		<script>
			$(document).ready(function() {
						    var progressBar = $('#progress-bar');  // 获取进度条元素
						    var intervalId = setInterval(function() {
						        $.ajax({
						            url: "{% url 'portscan_progress' %}",
						            type: "POST",
						            dataType: "json",
						            success: function(data) {
						                console.log(data);

						                if (data.task_state == 'SUCCESS!!!') {
						                	var $table = $('#exampleTableToolbar');
						                    console.log('Task succeeded!');
						                    var open_ports = JSON.parse(data.open_ports);
						                    var table_data = [];
						                    console.log(open_ports)
						                    for (var i = 0; i < open_ports.length; i++) {
						                        var fields = open_ports[i].fields;
											  // 创建一个新对象，其属性名与表格列的"data-field"属性值匹配
											  var row = {
												url:fields.url,
												port:fields.port,
												scan_time:fields.scan_time,
												status:'<spam class="label label-primary">开启</span>'
											  };
											  table_data.push(row);
						                    }
						                    $table.bootstrapTable('load', table_data);
						                    //修改进度条宽度和文本内容
						                    $("#progress-bar").css('width', 1700 + 'px');
						                    $("#progress-bar").text("100%  扫描完成");
						                    clearInterval(intervalId);  // 停止定时器
						                }

						                else if (data.task_state == 'PENDING!!!') {
						                    console.log('Task is still running.');
						                    //获取当前进度条宽度和文本内容
						                    var currentWidth = $("#progress-bar").width();
						                    var currentText = $("#progress-bar").text();
						                    console.log(currentWidth)
						                    //增加10%的宽度和文本内容
						                    var newWidth = currentWidth + $("#progress-bar").parent().width() * 0.01;
						                    var newText = (newWidth / $("#progress-bar").parent().width() * 100).toFixed(0) + '%';
						                    console.log(newWidth)
						                    //修改进度条宽度和文本内容
						                    $("#progress-bar").css('width', newWidth + 'px');
						                    $("#progress-bar").text(newText);
						                }
						            },
						            error: function(jqXHR, textStatus, errorThrown) {
						                console.log("AJAX Error: " + textStatus + ", " + errorThrown);
						                // TODO: 处理请求失败的情况
						            }
						        });
						    }, 3000);
						});
		</script>
	</body>

</html>